﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Effects;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Media;
using System.Windows.Threading;
using System.Threading;

using Microsoft.Research.Kinect.Nui;
using Microsoft.Research.Kinect.Audio;
using Microsoft.Speech.AudioFormat;
using Microsoft.Speech.Recognition;
using System.Windows.Controls.Primitives;

using Coding4Fun.Kinect.Wpf;
using Coding4Fun.Kinect.Wpf.Controls;

namespace Hugo
{
    /// <summary>
    /// Interaction logic for PlayGamesPage.xaml
    /// </summary>
    public partial class PlayGamesPage
    {
        protected SoundPlayer beep = new SoundPlayer(Properties.Resources.beep);
        protected SoundPlayer win = new SoundPlayer(Properties.Resources.tada);

        private bool isHovering = false;
        protected UIElement selectedElement = null;
        protected List<Button> elements;

        HoverButton handCursorButton = HandCursor.getInstance().hand;
        private DispatcherTimer timer;

        public PlayGamesPage()
        {
            InitializeComponent();
            LayoutRoot.Height = SystemParameters.WorkArea.Height;
            LayoutRoot.Width = SystemParameters.WorkArea.Width;

            backImage.Name = "backImage";
            backImage.Source = new BitmapImage(new Uri("/Resources/images/back.png", UriKind.RelativeOrAbsolute));
            backImage.Tag = "Back";
            backImage.Width = double.NaN;
            backImage.Height = double.NaN;
            backImage.MaxWidth = 250;
            backImage.MaxHeight = 159;
            backImage.Stretch = Stretch.Uniform;
            backImage.MouseUp += new MouseButtonEventHandler(backImage_MouseUp);

            this.Loaded += new RoutedEventHandler(PlayGamesPage_Loaded);
            this.Unloaded += new RoutedEventHandler(PlayGamesPage_Unloaded);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            bool newHov = IsCursorOverIcon() || NavigationPage.getInstance().setCursorPosition(new Point(HandCursor.getInstance().getXPosition(),HandCursor.getInstance().getYPosition()));
            if (isHovering != newHov)
            {
                Console.WriteLine(newHov);
            }
            HandCursor.getInstance().setHovering(newHov);
            isHovering = newHov;
        }
        

        void PlayGamesPage_Loaded(object sender, RoutedEventArgs e)
        {
            //initialize hand cursor
            InitializeHandCursor();
           
        }

        private void backImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            // TODO: Add event handler implementation here.
            beep.Play();
            Thread.Sleep(400);
            MainWindow.popPage();
        }

         private void InitializeHandCursor()
        {
            //register click event of the hand cursor (coding4fun hover button control)
            handCursorButton.Click += new RoutedEventHandler(handCursorButton_Click);
        }

        void handCursorButton_Click(object sender, RoutedEventArgs e)
        {
            if (selectedElement == null) { return; }
            if (selectedElement.GetType() == typeof(Button))
                selectedElement.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, selectedElement));
            else
            {
                MouseButtonEventArgs me = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left, null);
                me.RoutedEvent = Mouse.MouseUpEvent;
                selectedElement.RaiseEvent(me);
            }
        }

        public bool IsCursorOverIcon()
        {
            if (PresentationSource.FromVisual(handCursorButton) != null)
            {
                //Hand Cursor pointer (finger tip) location
                Point cursorTopLeft = handCursorButton.PointToScreen(new Point());
                double cursorCenterX = cursorTopLeft.X + (handCursorButton.RenderSize.Width / 2);
                double cursorTopY = cursorTopLeft.Y;

                Point imageTopLeft;
                double imageLeft;
                double imageRight;
                double imageTop;
                double imageBottom;

                //check if one of the buttons is selected
                foreach (Button target in elements)
                {

                    imageTopLeft = target.PointToScreen(new Point());
                    imageLeft = imageTopLeft.X;
                    imageRight = imageLeft + target.RenderSize.Width;
                    imageTop = imageTopLeft.Y;
                    imageBottom = imageTop + target.RenderSize.Height;

                    if (cursorCenterX > imageLeft - 15 && cursorCenterX < imageRight + 15
                          && cursorTopY > imageTop - 15 & cursorTopY < imageBottom + 15)
                    {
                        selectedElement = target;
                        return true;
                    }

                }
                //check if back button is selected
                if (backImage.IsEnabled)
                {
                    imageTopLeft = backImage.PointToScreen(new Point());
                    imageLeft = imageTopLeft.X;
                    imageRight = imageLeft + backImage.RenderSize.Width;
                    imageTop = imageTopLeft.Y;
                    imageBottom = imageTop + backImage.RenderSize.Height;

                    if (cursorCenterX > imageLeft - 25 && cursorCenterX < imageRight + 25
                          && cursorTopY > imageTop - 25 & cursorTopY < imageBottom + 25)
                    {
                        selectedElement = backImage;
                        return true;
                    }
                }
            }
            return false;
        }


        void PlayGamesPage_Unloaded(object sender, RoutedEventArgs e)
        {
        }

        internal void startTracking()
        {
            stopTracking();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(10);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        internal void stopTracking()
        {
            if (timer != null)
            {
                timer.Stop();
            }
        }
    }
}